﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Microsoft.WVB.Framework
{
    // TODO: REWIRTE with Generics ...
    public delegate void CreateUserEventHandler<T>(AcquireCompletedEventArgs<T> e) where T: class;
    public delegate void GetUserEventHandler<T>(AcquireCompletedEventArgs<T> e) where T: class;
    
    public abstract class MembershipProvider<T> : IMembershipProvider<T> where T:class, IMembershipUser
    {
        protected Dictionary<object, AsyncOperation> userStateLifetime = null;
        protected string serviceRoot { get; private set; }

        public MembershipProvider(string path)
        {
            serviceRoot = path;
            Initialize();
        }

        protected virtual void Initialize()
        {
            userStateLifetime = new Dictionary<object, AsyncOperation>();
        }

        public event GetUserEventHandler<T> GetUserCompleted;
        public abstract void GetUserAsync(string username, object userData);
        public void GetUserAsync(string username) { GetUserAsync(username, null); }
        public void GetUserAsync(object primaryKey) { GetUserAsync(primaryKey, null); }
        public abstract void GetUserAsync(object primaryKey, object userData);
        protected virtual void OnGetUserCompleted(AcquireCompletedEventArgs<T> e)
        {
            if (GetUserCompleted != null)
            {
                GetUserCompleted(e);
            }
        }

        public event CreateUserEventHandler<T> CreateUserCompleted;
        public abstract void CreateUserAsync(string username, string password, string email, object userData);
        public void CreateUserAsync(string username, string password, string email) { CreateUserAsync(username, password, email, null); }
        protected virtual void OnCreateUserCompleted(AcquireCompletedEventArgs<T> e)
        {
            if (CreateUserCompleted != null)
            {
                CreateUserCompleted(e);
            }
        }

        public event EventHandler<NotifyActionCompletedEventArgs> DeleteUserCompleted;
        public abstract void DeleteUserAsync(string username, object userData);
        public void DeleteUserAsync(string username) { DeleteUserAsync(username, null); }
        protected virtual void OnDeleteUserCompleted(NotifyActionCompletedEventArgs e)
        {
            EventHandler<NotifyActionCompletedEventArgs> handler = this.DeleteUserCompleted;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler<NotifyActionCompletedEventArgs> UpdateUserCompleted;
        public abstract void UpdateUserAsync(T user, object userData);
        public void UpdateUserAsync(T user) { UpdateUserAsync(user, null); }
        protected virtual void OnUpdateUserCompleted(NotifyActionCompletedEventArgs e)
        {
            if (UpdateUserCompleted != null)
            {
                UpdateUserCompleted(this, e);
            }
        }

        public event EventHandler<NotifyActionCompletedEventArgs> ValidateUserCompleted;
        public abstract void ValidateUserAsync(string username, string password, object userData);
        public void ValidateUserAsync(string username, string password) { ValidateUserAsync(username, password, null); }
        protected virtual void OnValidateUserCompleted(NotifyActionCompletedEventArgs e)
        {
            if (ValidateUserCompleted != null)
            {
                ValidateUserCompleted(this, e);
            }
        }

        public event EventHandler<NotifyActionCompletedEventArgs> ChangePasswordCompleted;
        public abstract void ChangePasswordAsync(string username, string oldpassword, string newpassword, object userData);
        public void ChangePasswordAsync(string username, string oldpassword, string newpassword) { ChangePasswordAsync(username, oldpassword, newpassword, null); }
        protected virtual void OnChangePasswordCompleted(NotifyActionCompletedEventArgs e)
        {
            if (ChangePasswordCompleted != null)
            {
                ChangePasswordCompleted(this, e);
            }
        }

        public abstract void CancelAsync();
    }
}
